using StudentWeb.Common;
using StudentWeb.IDAL;
using StudentWeb.Model;

namespace StudentWeb.BLL
{
    public record WYUserInfoParam
    (
        int WYUserID,
        string WYUserName,
        string WYPassword,
        string WYEmail,
        string WYPhone,
        string WYAddress
    );

    /// <summary>
    /// 业务逻辑层（Business Object），负责处理医保用户相关的业务逻辑
    /// </summary>
    [Service(EServLife.Scoped)]
    public class WYUserBO(IWYUserInfoDAO wyUserInfoDAO)
    {
        #region 分页查询
        public IResult PagedList(int pageIndex, int pageSize, dynamic condition)
        {
            string? where = null;

            if (condition != null)
            {
                if (condition.WYUserName != null)
                {
                    where = $"WYUserName like '%'+@WYUserName+'%'";
                }
                if (condition.WYPhone != null)
                {
                    if (where != null) where += " and ";
                    where += $"WYPhone like '%'+@WYPhone+'%'";
                }
                if (condition.WYEmail != null)
                {
                    if (where != null) where += " and ";
                    where += $"WYEmail like '%'+@WYEmail+'%'";
                }
            }

            var param = BllHelper.ToParamObj(condition);
            var list = wyUserInfoDAO.GetList(pageIndex, pageSize, where, param);
            return Result.Data(list);
        }
        #endregion

        #region 查询单条
        public IResult GetInfo(int wYUserID)
        {
            var res = wyUserInfoDAO.GetInfo(wYUserID);
            return Result.Data(res);
        }
        #endregion

        #region 添加单条
        public IResult Insert(WYUserInfoParam model)
        {
            var record = new WYUserInfo
            {
                WYUserName = model.WYUserName,
                WYPassword = model.WYPassword,
                WYEmail = model.WYEmail,
                WYPhone = model.WYPhone,
                WYAddress = model.WYAddress,
            };

            int ret = wyUserInfoDAO.Insert(record);
            return ret > 0 ? Result.OK("添加成功", new { record.WYUserID }) : Result.Fail("添加失败");
        }
        #endregion

        #region 修改单条
        public IResult Update(WYUserInfoParam model)
        {
            var record = wyUserInfoDAO.GetInfo(model.WYUserID);
            if (record == null) return Result.Fail("用户不存在");

            record.WYUserName = model.WYUserName;
            record.WYPassword = model.WYPassword;
            record.WYEmail = model.WYEmail;
            record.WYPhone = model.WYPhone;
            record.WYAddress = model.WYAddress;

            int ret = wyUserInfoDAO.Update(record);
            return ret > 0 ? Result.OK("修改成功", new { record.WYUserID }) : Result.Fail("修改失败");
        }
        #endregion

        #region 删除单条
        public IResult Delete(int wYUserID)
        {
            var res = wyUserInfoDAO.Delete(wYUserID);
            return Result.Data(res);
        }
        #endregion
    }
}



